﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;

using System.Data;
namespace SharpMap.Wpf
{
    /// <summary>
    /// Interaction logic for LayerFeaturesTable.xaml
    /// </summary>
    public partial class LayerFeaturesTable : UserControl
    {
        public LayerFeaturesTable()
        {
            InitializeComponent();
        }



        public IEnumerable ItemsSource
        {
            get { return (IEnumerable)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(LayerFeaturesTable),
            new FrameworkPropertyMetadata(default(IEnumerable), ItemsSourcePropertyChanged)
            );



        static void ItemsSourcePropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            LayerFeaturesTable This = target as LayerFeaturesTable;
            This.UpdateColumns(e);
        }

        private void UpdateColumns(DependencyPropertyChangedEventArgs e)
        {
            DataView dataView = ItemsSource as System.Data.DataView;
            if (dataView != null)
            {
                GridView gridView = _listView.View as GridView;
                if(gridView == null)
                {
                    _listView.View= gridView = new GridView();
                }
                gridView.Columns.Clear();
                foreach (DataColumn dc in dataView.Table.Columns)
                {
                    GridViewColumn gvc = new GridViewColumn();
                    gvc.Header = dc.Caption;
                    gvc.DisplayMemberBinding = new Binding(dc.Caption);
                    gridView.Columns.Add(gvc);
                }
            }
        }



        
    }
}
